﻿using Infrastructure.Repository.Sys;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;

namespace CoreDomain.BusinessCore
{
    /// <summary>
    /// 菜单核心
    /// </summary>
    public static class MenuCore
    {
        /// <summary>
        /// 菜单仓储
        /// </summary>
        private static SysMenuRepository  _sysMenuRepository = new();

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        private static List<SysMenu> GetALLMenuByWhere(string corporationKey, string systemKey)
        {
            var menuList = _sysMenuRepository.GetMenuByCorporationKeyOrSystemKey(corporationKey, systemKey);
            return menuList;
        }

        /// <summary>
        /// 递归获取菜单,组成树形结构
        /// </summary>
        /// <param name="menuList">菜单数据</param>
        /// <returns>返回菜单的树形结构</returns>
        public static List<SysMenuOutPut> GetMenuTreeList(List<SysMenuOutPut> menuList)
        {
           // List<SysMenuOutPut> list = new();
            List<SysMenuOutPut> menuListDto = new();
            ////模型的转换
            //foreach (var item in menuList)
            //{
            //    SysMenuOutPut model = new()
            //    {
            //        Id = item.Id,
            //        Pid = item.Pid,
            //        CorporationKey = item.CorporationKey,
            //        SystemKey = item.SystemKey,
            //        Path = item.MenuUrl,
            //        Name = item.MenuTitle,
            //        MenuIcon = item.MenuIcon,
            //        Component = item.Component,
            //        IsOpen = item.IsOpen,
            //        Sort = item.Sort,
            //        RequireAuth = item.RequireAuth,
            //        Redirect = item.Redirect,
            //        CreateTime = item.CreateTime,
            //        CreateUser = item.CreateUser,
            //    };
            //    list.Add(model);
            //}
            //递归所有父级菜单
            foreach (var data in menuList.Where(f => f.Pid == "0" && f.IsOpen).OrderBy(f=>f.Sort).ThenBy(f=>f.CreateTime))
            {
                var childrenList = GetChildrenMenu(menuList, data.Id).OrderBy(f => f.Sort).ThenBy(f => f.CreateTime).ToList();
                data.Children = childrenList.Count == 0 ? null : childrenList;
                menuListDto.Add(data);
            }
            return menuListDto;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="moduleOutput">菜单数据</param>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        private static List<SysMenuOutPut> GetChildrenMenu(List<SysMenuOutPut> moduleOutput, string? id)
        {
            List<SysMenuOutPut> sysShowTempMenus = new();
            //得到子菜单
            var info = moduleOutput.Where(w => w.Pid.ToLower() == id.ToLower() && w.IsOpen).ToList();
            //循环
            foreach (var sysMenuInfo in info)
            {
                var childrenList = GetChildrenMenu(moduleOutput, sysMenuInfo.Id);
                //把子菜单放到Children集合里
                sysMenuInfo.Children = childrenList.Count == 0 ? null : childrenList;
                //添加父级菜单
                sysShowTempMenus.Add(sysMenuInfo);
            }
            return sysShowTempMenus;
        }

        /// <summary>
        /// 根据菜单id获取所有子集菜单id
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns>返回所有下级菜单</returns>
        public static List<string> GetChildrenRoleById(string menuId, string corporationKey, string systemKey)
        {
            List<string> idList = [menuId];
            var menuList = GetALLMenuByWhere(corporationKey, systemKey);
            foreach (var item in menuList)
            {
                if (menuId.ToLower() == item.Pid.ToLower())
                {
                    idList.Add(item.Id);
                    idList.AddRange(GetChildrenRoletId(item.Id, menuList));
                }
            }
            return idList;
        }

        /// <summary>
        /// 实现递归
        /// </summary>
        /// <param name="menuId">上级菜单ID</param>
        /// <param name="sysMenuList">所有菜单集合</param>
        /// <returns></returns>
        private static List<string> GetChildrenRoletId(string menuId, List<SysMenu> sysMenuList)
        {
            List<string> idList = new();
            foreach (var item in sysMenuList)
            {
                if (menuId == item.Pid)
                {
                    idList.Add(item.Id);
                    GetChildrenRoletId(item.Id, sysMenuList);
                }
            }
            return idList;
        }
    }
}
